{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 3.1.2. 矢量化加速¶"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "21b3326bb502f528"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import math\n",
    "import time\n",
    "import numpy as np\n",
    "import torch\n",
    "from d2l import torch as d2l"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T08:34:15.308137Z",
     "start_time": "2024-03-28T08:34:10.997512Z"
    }
   },
   "id": "e7b9fb9d8e67ced2",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "n = 10000\n",
    "a = torch.ones([n])\n",
    "b = torch.ones([n])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T08:34:29.164451Z",
     "start_time": "2024-03-28T08:34:29.148934Z"
    }
   },
   "id": "ce467fb86a6e3d0e",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class Timer:  #@save\n",
    "    \"\"\"记录多次运行时间\"\"\"\n",
    "    def __init__(self):\n",
    "        self.times = []\n",
    "        self.start()\n",
    "\n",
    "    def start(self):\n",
    "        \"\"\"启动计时器\"\"\"\n",
    "        self.tik = time.time()\n",
    "\n",
    "    def stop(self):\n",
    "        \"\"\"停止计时器并将时间记录在列表中\"\"\"\n",
    "        self.times.append(time.time() - self.tik)\n",
    "        return self.times[-1]\n",
    "\n",
    "    def avg(self):\n",
    "        \"\"\"返回平均时间\"\"\"\n",
    "        return sum(self.times) / len(self.times)\n",
    "\n",
    "    def sum(self):\n",
    "        \"\"\"返回时间总和\"\"\"\n",
    "        return sum(self.times)\n",
    "\n",
    "    def cumsum(self):\n",
    "        \"\"\"返回累计时间\"\"\"\n",
    "        return np.array(self.times).cumsum().tolist()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T08:34:44.807272Z",
     "start_time": "2024-03-28T08:34:44.793827Z"
    }
   },
   "id": "1855afa76387f0eb",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'0.16282 sec'"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = torch.zeros(n)\n",
    "timer = Timer()\n",
    "for i in range(n):\n",
    "    c[i] = a[i] + b[i]\n",
    "f'{timer.stop():.5f} sec'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T08:35:38.437084Z",
     "start_time": "2024-03-28T08:35:38.265743Z"
    }
   },
   "id": "ce516bce2fa6f9e2",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'0.00100 sec'"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "timer.start()\n",
    "d = a + b\n",
    "f'{timer.stop():.5f} sec'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T08:35:53.944591Z",
     "start_time": "2024-03-28T08:35:53.932520Z"
    }
   },
   "id": "1b6c2c7d660059b7",
   "execution_count": 5
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.1.3. 正态分布与平方损失"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "75f96d67f6039642"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def normal(x, mu, sigma):\n",
    "    p = 1 / math.sqrt(2 * math.pi * sigma**2)\n",
    "    return p * np.exp(-0.5 / sigma**2 * (x - mu)**2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T08:38:59.899751Z",
     "start_time": "2024-03-28T08:38:59.875247Z"
    }
   },
   "id": "c3f6737e542bb28a",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 450x250 with 1 Axes>",
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"300.5625pt\" height=\"180.25pt\" viewBox=\"0 0 300.5625 180.25\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2024-03-28T16:39:44.600540</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.2, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 180.25 \nL 300.5625 180.25 \nL 300.5625 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 42.2625 145.8 \nL 293.3625 145.8 \nL 293.3625 7.2 \nL 42.2625 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <path d=\"M 69.992986 145.8 \nL 69.992986 7.2 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_2\">\n      <defs>\n       <path id=\"m8812f540ef\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m8812f540ef\" x=\"69.992986\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- -6 -->\n      <g transform=\"translate(64.992986 159.675) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"SimHei-2d\" d=\"M 2975 2125 \nL 125 2125 \nL 125 2525 \nL 2975 2525 \nL 2975 2125 \nz\n\" transform=\"scale(0.015625)\"/>\n        <path id=\"SimHei-36\" d=\"M 250 1612 \nQ 275 1975 387 2225 \nQ 500 2475 725 2850 \nL 1750 4450 \nL 2325 4450 \nL 1275 2800 \nQ 1950 2975 2350 2750 \nQ 2750 2525 2887 2237 \nQ 3025 1950 3037 1612 \nQ 3050 1275 2937 950 \nQ 2825 625 2537 362 \nQ 2250 100 1737 75 \nQ 1225 50 862 262 \nQ 500 475 362 862 \nQ 225 1250 250 1612 \nz\nM 1025 787 \nQ 1250 550 1625 525 \nQ 2000 500 2250 775 \nQ 2500 1050 2500 1575 \nQ 2500 2100 2187 2300 \nQ 1875 2500 1487 2450 \nQ 1100 2400 925 2075 \nQ 750 1750 775 1387 \nQ 800 1025 1025 787 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#SimHei-2d\"/>\n       <use xlink:href=\"#SimHei-36\" x=\"50\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_3\">\n      <path d=\"M 102.626685 145.8 \nL 102.626685 7.2 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#m8812f540ef\" x=\"102.626685\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- -4 -->\n      <g transform=\"translate(97.626685 159.675) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"SimHei-34\" d=\"M 2000 1100 \nL 75 1100 \nL 75 1525 \nL 2100 4450 \nL 2475 4450 \nL 2475 1525 \nL 3075 1525 \nL 3075 1100 \nL 2475 1100 \nL 2475 150 \nL 2000 150 \nL 2000 1100 \nz\nM 2000 1525 \nL 2000 3500 \nL 600 1525 \nL 2000 1525 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#SimHei-2d\"/>\n       <use xlink:href=\"#SimHei-34\" x=\"50\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_5\">\n      <path d=\"M 135.260385 145.8 \nL 135.260385 7.2 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#m8812f540ef\" x=\"135.260385\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- -2 -->\n      <g transform=\"translate(130.260385 159.675) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"SimHei-32\" d=\"M 300 250 \nQ 325 625 650 925 \nQ 975 1225 1475 1862 \nQ 1975 2500 2125 2850 \nQ 2275 3200 2237 3450 \nQ 2200 3700 2000 3862 \nQ 1800 4025 1537 4000 \nQ 1275 3975 1037 3800 \nQ 800 3625 675 3275 \nL 200 3350 \nQ 400 3925 712 4187 \nQ 1025 4450 1450 4475 \nQ 1700 4500 1900 4462 \nQ 2100 4425 2312 4287 \nQ 2525 4150 2662 3875 \nQ 2800 3600 2762 3212 \nQ 2725 2825 2375 2287 \nQ 2025 1750 1025 600 \nL 2825 600 \nL 2825 150 \nL 300 150 \nL 300 250 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#SimHei-2d\"/>\n       <use xlink:href=\"#SimHei-32\" x=\"50\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_7\">\n      <path d=\"M 167.894084 145.8 \nL 167.894084 7.2 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_8\">\n      <g>\n       <use xlink:href=\"#m8812f540ef\" x=\"167.894084\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 0 -->\n      <g transform=\"translate(165.394084 159.675) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"SimHei-30\" d=\"M 225 2537 \nQ 250 3200 412 3587 \nQ 575 3975 875 4225 \nQ 1175 4475 1612 4475 \nQ 2050 4475 2375 4112 \nQ 2700 3750 2800 3200 \nQ 2900 2650 2862 1937 \nQ 2825 1225 2612 775 \nQ 2400 325 1975 150 \nQ 1550 -25 1125 187 \nQ 700 400 525 750 \nQ 350 1100 275 1487 \nQ 200 1875 225 2537 \nz\nM 750 2687 \nQ 675 2000 800 1462 \nQ 925 925 1212 700 \nQ 1500 475 1800 612 \nQ 2100 750 2237 1162 \nQ 2375 1575 2375 2062 \nQ 2375 2550 2337 2950 \nQ 2300 3350 2112 3675 \nQ 1925 4000 1612 4012 \nQ 1300 4025 1062 3700 \nQ 825 3375 750 2687 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#SimHei-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_9\">\n      <path d=\"M 200.527784 145.8 \nL 200.527784 7.2 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#m8812f540ef\" x=\"200.527784\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 2 -->\n      <g transform=\"translate(198.027784 159.675) scale(0.1 -0.1)\">\n       <use xlink:href=\"#SimHei-32\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_6\">\n     <g id=\"line2d_11\">\n      <path d=\"M 233.161483 145.8 \nL 233.161483 7.2 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_12\">\n      <g>\n       <use xlink:href=\"#m8812f540ef\" x=\"233.161483\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 4 -->\n      <g transform=\"translate(230.661483 159.675) scale(0.1 -0.1)\">\n       <use xlink:href=\"#SimHei-34\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_7\">\n     <g id=\"line2d_13\">\n      <path d=\"M 265.795182 145.8 \nL 265.795182 7.2 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_14\">\n      <g>\n       <use xlink:href=\"#m8812f540ef\" x=\"265.795182\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 6 -->\n      <g transform=\"translate(263.295182 159.675) scale(0.1 -0.1)\">\n       <use xlink:href=\"#SimHei-36\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_8\">\n     <!-- x -->\n     <g transform=\"translate(165.3125 171.8) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"SimHei-78\" d=\"M 2875 125 \nL 2275 125 \nL 1550 1200 \nL 825 125 \nL 225 125 \nL 1250 1550 \nL 300 2925 \nL 900 2925 \nL 1550 1875 \nL 2200 2925 \nL 2800 2925 \nL 1850 1550 \nL 2875 125 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#SimHei-78\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_15\">\n      <path d=\"M 42.2625 139.5 \nL 293.3625 139.5 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_16\">\n      <defs>\n       <path id=\"mda2444a41c\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mda2444a41c\" x=\"42.2625\" y=\"139.5\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 0.0 -->\n      <g transform=\"translate(20.2625 142.9375) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"SimHei-2e\" d=\"M 1075 125 \nL 500 125 \nL 500 675 \nL 1075 675 \nL 1075 125 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#SimHei-30\"/>\n       <use xlink:href=\"#SimHei-2e\" x=\"50\"/>\n       <use xlink:href=\"#SimHei-30\" x=\"100\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_17\">\n      <path d=\"M 42.2625 107.916484 \nL 293.3625 107.916484 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_18\">\n      <g>\n       <use xlink:href=\"#mda2444a41c\" x=\"42.2625\" y=\"107.916484\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 0.1 -->\n      <g transform=\"translate(20.2625 111.353984) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"SimHei-31\" d=\"M 1400 3600 \nQ 1075 3275 575 2975 \nL 575 3450 \nQ 1200 3875 1600 4450 \nL 1900 4450 \nL 1900 150 \nL 1400 150 \nL 1400 3600 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#SimHei-30\"/>\n       <use xlink:href=\"#SimHei-2e\" x=\"50\"/>\n       <use xlink:href=\"#SimHei-31\" x=\"100\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_19\">\n      <path d=\"M 42.2625 76.332967 \nL 293.3625 76.332967 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_20\">\n      <g>\n       <use xlink:href=\"#mda2444a41c\" x=\"42.2625\" y=\"76.332967\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_11\">\n      <!-- 0.2 -->\n      <g transform=\"translate(20.2625 79.770467) scale(0.1 -0.1)\">\n       <use xlink:href=\"#SimHei-30\"/>\n       <use xlink:href=\"#SimHei-2e\" x=\"50\"/>\n       <use xlink:href=\"#SimHei-32\" x=\"100\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_21\">\n      <path d=\"M 42.2625 44.749451 \nL 293.3625 44.749451 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_22\">\n      <g>\n       <use xlink:href=\"#mda2444a41c\" x=\"42.2625\" y=\"44.749451\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_12\">\n      <!-- 0.3 -->\n      <g transform=\"translate(20.2625 48.186951) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"SimHei-33\" d=\"M 250 1225 \nL 700 1300 \nQ 800 975 1025 762 \nQ 1250 550 1587 562 \nQ 1925 575 2125 837 \nQ 2325 1100 2300 1437 \nQ 2275 1775 2037 1962 \nQ 1800 2150 1275 2225 \nL 1275 2550 \nQ 1800 2600 2037 2825 \nQ 2275 3050 2250 3412 \nQ 2225 3775 1925 3937 \nQ 1625 4100 1287 3975 \nQ 950 3850 750 3275 \nL 300 3350 \nQ 450 3800 712 4100 \nQ 975 4400 1425 4450 \nQ 1875 4500 2212 4337 \nQ 2550 4175 2687 3837 \nQ 2825 3500 2725 3100 \nQ 2625 2700 2150 2400 \nQ 2500 2250 2687 1950 \nQ 2875 1650 2812 1162 \nQ 2750 675 2375 375 \nQ 2000 75 1525 87 \nQ 1050 100 700 387 \nQ 350 675 250 1225 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#SimHei-30\"/>\n       <use xlink:href=\"#SimHei-2e\" x=\"50\"/>\n       <use xlink:href=\"#SimHei-33\" x=\"100\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_5\">\n     <g id=\"line2d_23\">\n      <path d=\"M 42.2625 13.165935 \nL 293.3625 13.165935 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_24\">\n      <g>\n       <use xlink:href=\"#mda2444a41c\" x=\"42.2625\" y=\"13.165935\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_13\">\n      <!-- 0.4 -->\n      <g transform=\"translate(20.2625 16.603435) scale(0.1 -0.1)\">\n       <use xlink:href=\"#SimHei-30\"/>\n       <use xlink:href=\"#SimHei-2e\" x=\"50\"/>\n       <use xlink:href=\"#SimHei-34\" x=\"100\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_14\">\n     <!-- p(x) -->\n     <g transform=\"translate(15.0125 86.5) rotate(-90) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"SimHei-70\" d=\"M 2825 1525 \nQ 2825 800 2475 425 \nQ 2125 50 1625 50 \nQ 1350 50 1150 162 \nQ 950 275 800 500 \nL 800 -800 \nL 300 -800 \nL 300 2925 \nL 800 2925 \nL 800 2550 \nQ 950 2775 1150 2875 \nQ 1350 2975 1625 2975 \nQ 2125 2975 2475 2612 \nQ 2825 2250 2825 1525 \nz\nM 2275 1525 \nQ 2275 2000 2087 2262 \nQ 1900 2525 1525 2525 \nQ 1225 2525 1012 2262 \nQ 800 2000 800 1525 \nQ 800 1050 1012 775 \nQ 1225 500 1525 500 \nQ 1900 500 2087 775 \nQ 2275 1050 2275 1525 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-28\" d=\"M 2975 -200 \nL 2700 -475 \nQ 2075 125 1762 775 \nQ 1450 1425 1450 2250 \nQ 1450 3075 1762 3725 \nQ 2075 4375 2700 5000 \nL 2975 4725 \nQ 2400 4175 2112 3587 \nQ 1825 3000 1825 2250 \nQ 1825 1500 2112 912 \nQ 2400 325 2975 -200 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-29\" d=\"M 1675 2250 \nQ 1675 1425 1362 775 \nQ 1050 125 425 -475 \nL 150 -200 \nQ 725 325 1012 912 \nQ 1300 1500 1300 2250 \nQ 1300 3000 1012 3587 \nQ 725 4175 150 4725 \nL 425 5000 \nQ 1050 4375 1362 3725 \nQ 1675 3075 1675 2250 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#SimHei-70\"/>\n      <use xlink:href=\"#SimHei-28\" x=\"50\"/>\n      <use xlink:href=\"#SimHei-78\" x=\"100\"/>\n      <use xlink:href=\"#SimHei-29\" x=\"150\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_25\">\n    <path d=\"M 53.676136 139.5 \nL 106.542729 139.392742 \nL 111.92729 139.148719 \nL 115.516997 138.770771 \nL 118.290861 138.25954 \nL 120.57522 137.619981 \nL 122.533242 136.856558 \nL 124.328096 135.932618 \nL 125.959781 134.864093 \nL 127.591465 133.53546 \nL 129.22315 131.902404 \nL 130.854835 129.918522 \nL 132.323352 127.79423 \nL 133.955037 125.01546 \nL 135.586722 121.755388 \nL 137.218407 117.977866 \nL 138.850092 113.655911 \nL 140.644945 108.255858 \nL 142.602967 101.596938 \nL 144.724158 93.525729 \nL 147.171685 83.248545 \nL 150.271887 69.178196 \nL 156.309121 41.57206 \nL 158.430311 33.006718 \nL 160.225165 26.675681 \nL 161.693681 22.276554 \nL 162.999029 19.044317 \nL 164.141209 16.789011 \nL 165.12022 15.307609 \nL 165.936062 14.403942 \nL 166.751905 13.808322 \nL 167.404579 13.556687 \nL 168.057253 13.5063 \nL 168.709927 13.657402 \nL 169.362601 14.009268 \nL 170.178443 14.728769 \nL 170.994286 15.753897 \nL 171.973297 17.376612 \nL 173.115476 19.788832 \nL 174.257656 22.726997 \nL 175.563004 26.675681 \nL 177.194689 32.392617 \nL 179.152711 40.191426 \nL 181.600238 50.957567 \nL 190.737674 92.210802 \nL 193.022033 101.006788 \nL 194.980055 107.731288 \nL 196.938077 113.655911 \nL 198.73293 118.379758 \nL 200.364615 122.104146 \nL 201.9963 125.314336 \nL 203.627985 128.047227 \nL 205.25967 130.345625 \nL 206.891355 132.255577 \nL 208.52304 133.824084 \nL 210.154725 135.097239 \nL 211.949578 136.208702 \nL 213.9076 137.136646 \nL 216.028791 137.875773 \nL 218.31315 138.435791 \nL 221.087014 138.879679 \nL 224.513553 139.193999 \nL 229.408608 139.396705 \nL 237.893369 139.487295 \nL 275.585292 139.5 \nL 281.948864 139.5 \nL 281.948864 139.5 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_26\">\n    <path d=\"M 53.676136 139.362188 \nL 64.11892 139.098708 \nL 71.461503 138.699787 \nL 77.1724 138.178279 \nL 82.067455 137.516853 \nL 86.473004 136.697191 \nL 90.389048 135.745999 \nL 93.978755 134.654637 \nL 97.405294 133.387507 \nL 100.668663 131.951607 \nL 103.932033 130.271147 \nL 107.195403 128.328848 \nL 110.458773 126.112322 \nL 113.722143 123.615644 \nL 116.985513 120.840886 \nL 120.412052 117.640793 \nL 124.164927 113.829699 \nL 128.570476 109.01843 \nL 134.607711 102.052696 \nL 142.929304 92.482389 \nL 146.845348 88.331596 \nL 150.108718 85.194678 \nL 152.882583 82.824913 \nL 155.493278 80.888277 \nL 157.777637 79.455574 \nL 160.061996 78.288522 \nL 162.183187 77.457339 \nL 164.141209 76.915213 \nL 166.099231 76.595215 \nL 168.057253 76.500787 \nL 170.015275 76.632947 \nL 171.973297 76.99027 \nL 173.931319 77.568916 \nL 176.052509 78.438306 \nL 178.1737 79.54932 \nL 180.458059 81.000265 \nL 182.905586 82.824913 \nL 185.516282 85.047175 \nL 188.453315 87.839952 \nL 191.879853 91.412502 \nL 196.285403 96.3499 \nL 211.460073 113.657453 \nL 215.376117 117.640793 \nL 218.965824 120.986092 \nL 222.392362 123.877929 \nL 225.655732 126.346542 \nL 228.919102 128.535259 \nL 232.182472 130.450724 \nL 235.445842 132.105883 \nL 238.709212 133.51842 \nL 242.13575 134.763387 \nL 245.725457 135.834289 \nL 249.641501 136.766392 \nL 253.883882 137.542786 \nL 258.615769 138.178279 \nL 264.163497 138.687887 \nL 270.853406 139.065638 \nL 279.501336 139.318239 \nL 281.948864 139.359757 \nL 281.948864 139.359757 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"line2d_27\">\n    <path d=\"M 53.676136 139.5 \nL 155.493278 139.392742 \nL 160.877839 139.148719 \nL 164.467546 138.770771 \nL 167.24141 138.25954 \nL 169.525769 137.619981 \nL 171.483791 136.856558 \nL 173.278645 135.932618 \nL 174.91033 134.864093 \nL 176.542015 133.53546 \nL 178.1737 131.902404 \nL 179.805385 129.918522 \nL 181.273901 127.79423 \nL 182.905586 125.01546 \nL 184.537271 121.755388 \nL 186.168956 117.977866 \nL 187.800641 113.655911 \nL 189.595494 108.255858 \nL 191.553516 101.596938 \nL 193.674707 93.525729 \nL 196.122234 83.248545 \nL 199.222436 69.178196 \nL 205.25967 41.57206 \nL 207.380861 33.006718 \nL 209.175714 26.675681 \nL 210.64423 22.276554 \nL 211.949578 19.044317 \nL 213.091758 16.789011 \nL 214.070769 15.307609 \nL 214.886611 14.403942 \nL 215.702454 13.808322 \nL 216.355128 13.556687 \nL 217.007802 13.5063 \nL 217.660476 13.657402 \nL 218.31315 14.009268 \nL 219.128992 14.728769 \nL 219.944835 15.753897 \nL 220.923846 17.376612 \nL 222.066025 19.788832 \nL 223.208205 22.726997 \nL 224.513553 26.675681 \nL 226.145238 32.392617 \nL 228.10326 40.191426 \nL 230.550787 50.957567 \nL 239.688223 92.210802 \nL 241.972582 101.006788 \nL 243.930604 107.731288 \nL 245.888626 113.655911 \nL 247.683479 118.379758 \nL 249.315164 122.104146 \nL 250.946849 125.314336 \nL 252.578534 128.047227 \nL 254.210219 130.345625 \nL 255.841904 132.255577 \nL 257.473589 133.824084 \nL 259.105274 135.097239 \nL 260.900128 136.208702 \nL 262.858149 137.136646 \nL 264.97934 137.875773 \nL 267.263699 138.435791 \nL 270.037563 138.879679 \nL 273.464102 139.193999 \nL 278.359157 139.396705 \nL 281.948864 139.456009 \nL 281.948864 139.456009 \n\" clip-path=\"url(#p128314f404)\" style=\"fill: none; stroke-dasharray: 9.6,2.4,1.5,2.4; stroke-dashoffset: 0; stroke: #008000; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 42.2625 145.8 \nL 42.2625 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 293.3625 145.8 \nL 293.3625 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 42.2625 145.8 \nL 293.3625 145.8 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 42.2625 7.2 \nL 293.3625 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"legend_1\">\n    <g id=\"patch_7\">\n     <path d=\"M 49.2625 54.95 \nL 146.2625 54.95 \nQ 148.2625 54.95 148.2625 52.95 \nL 148.2625 14.2 \nQ 148.2625 12.2 146.2625 12.2 \nL 49.2625 12.2 \nQ 47.2625 12.2 47.2625 14.2 \nL 47.2625 52.95 \nQ 47.2625 54.95 49.2625 54.95 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_28\">\n     <path d=\"M 51.2625 19.7 \nL 61.2625 19.7 \nL 71.2625 19.7 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_15\">\n     <!-- mean 0, std 1 -->\n     <g transform=\"translate(79.2625 23.2) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"SimHei-6d\" d=\"M 3050 125 \nL 2550 125 \nL 2550 2150 \nQ 2550 2300 2500 2400 \nQ 2450 2500 2300 2500 \nQ 2125 2500 1975 2312 \nQ 1825 2125 1825 1825 \nL 1825 125 \nL 1325 125 \nL 1325 2150 \nQ 1325 2300 1275 2400 \nQ 1225 2500 1075 2500 \nQ 900 2500 750 2312 \nQ 600 2125 600 1825 \nL 600 125 \nL 100 125 \nL 100 2925 \nL 600 2925 \nL 600 2550 \nQ 725 2750 900 2862 \nQ 1075 2975 1275 2975 \nQ 1475 2975 1612 2862 \nQ 1750 2750 1800 2550 \nQ 1925 2750 2087 2862 \nQ 2250 2975 2450 2975 \nQ 2750 2975 2900 2812 \nQ 3050 2650 3050 2350 \nL 3050 125 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-65\" d=\"M 2850 1075 \nQ 2800 625 2450 350 \nQ 2100 75 1625 75 \nQ 1025 75 637 462 \nQ 250 850 250 1525 \nQ 250 2200 637 2587 \nQ 1025 2975 1625 2975 \nQ 2150 2975 2487 2637 \nQ 2825 2300 2825 1525 \nL 800 1525 \nQ 800 975 1037 750 \nQ 1275 525 1625 525 \nQ 1900 525 2075 662 \nQ 2250 800 2300 1075 \nL 2850 1075 \nz\nM 2250 1925 \nQ 2200 2275 2025 2412 \nQ 1850 2550 1575 2550 \nQ 1325 2550 1125 2412 \nQ 925 2275 825 1925 \nL 2250 1925 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-61\" d=\"M 2875 125 \nL 2275 125 \nQ 2225 175 2200 262 \nQ 2175 350 2175 475 \nQ 2000 275 1750 175 \nQ 1500 75 1225 75 \nQ 825 75 550 275 \nQ 275 475 275 850 \nQ 275 1225 525 1450 \nQ 775 1675 1300 1750 \nQ 1650 1800 1912 1875 \nQ 2175 1950 2175 2075 \nQ 2175 2225 2062 2375 \nQ 1950 2525 1575 2525 \nQ 1275 2525 1137 2412 \nQ 1000 2300 950 2100 \nL 400 2100 \nQ 450 2500 762 2737 \nQ 1075 2975 1575 2975 \nQ 2125 2975 2400 2725 \nQ 2675 2475 2675 2025 \nL 2675 650 \nQ 2675 500 2725 375 \nQ 2775 250 2875 125 \nz\nM 2175 1050 \nL 2175 1550 \nQ 2025 1500 1887 1462 \nQ 1750 1425 1425 1375 \nQ 1050 1325 937 1200 \nQ 825 1075 825 900 \nQ 825 750 937 637 \nQ 1050 525 1275 525 \nQ 1500 525 1762 650 \nQ 2025 775 2175 1050 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-6e\" d=\"M 2800 125 \nL 2300 125 \nL 2300 1925 \nQ 2300 2225 2150 2400 \nQ 2000 2575 1750 2575 \nQ 1425 2575 1137 2237 \nQ 850 1900 850 1400 \nL 850 125 \nL 350 125 \nL 350 2925 \nL 850 2925 \nL 850 2400 \nQ 1050 2675 1287 2825 \nQ 1525 2975 1900 2975 \nQ 2350 2975 2575 2725 \nQ 2800 2475 2800 2100 \nL 2800 125 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-20\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-2c\" d=\"M 1100 125 \nL 775 -550 \nL 525 -550 \nL 775 125 \nL 525 125 \nL 525 675 \nL 1100 675 \nL 1100 125 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-73\" d=\"M 2750 900 \nQ 2750 500 2437 287 \nQ 2125 75 1650 75 \nQ 1050 75 725 312 \nQ 400 550 400 1000 \nL 900 1000 \nQ 900 700 1112 600 \nQ 1325 500 1625 500 \nQ 1925 500 2075 612 \nQ 2225 725 2225 900 \nQ 2225 1025 2100 1150 \nQ 1975 1275 1475 1350 \nQ 900 1425 687 1637 \nQ 475 1850 475 2200 \nQ 475 2500 762 2737 \nQ 1050 2975 1600 2975 \nQ 2100 2975 2387 2750 \nQ 2675 2525 2675 2150 \nL 2175 2150 \nQ 2175 2375 2012 2462 \nQ 1850 2550 1600 2550 \nQ 1275 2550 1137 2437 \nQ 1000 2325 1000 2175 \nQ 1000 2000 1125 1900 \nQ 1250 1800 1650 1750 \nQ 2300 1650 2525 1437 \nQ 2750 1225 2750 900 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-74\" d=\"M 2750 200 \nQ 2625 150 2462 112 \nQ 2300 75 2025 75 \nQ 1575 75 1300 325 \nQ 1025 575 1025 1025 \nL 1025 2525 \nL 175 2525 \nL 175 2925 \nL 1025 2925 \nL 1025 3900 \nL 1525 3900 \nL 1525 2925 \nL 2550 2925 \nL 2550 2525 \nL 1525 2525 \nL 1525 1000 \nQ 1525 800 1625 662 \nQ 1725 525 2000 525 \nQ 2275 525 2450 575 \nQ 2625 625 2750 700 \nL 2750 200 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"SimHei-64\" d=\"M 2750 125 \nL 2250 125 \nL 2250 500 \nQ 2100 275 1900 175 \nQ 1700 75 1425 75 \nQ 925 75 575 437 \nQ 225 800 225 1525 \nQ 225 2250 575 2625 \nQ 925 3000 1425 3000 \nQ 1700 3000 1900 2887 \nQ 2100 2775 2250 2550 \nL 2250 4400 \nL 2750 4400 \nL 2750 125 \nz\nM 2250 1525 \nQ 2250 2000 2037 2275 \nQ 1825 2550 1525 2550 \nQ 1150 2550 962 2275 \nQ 775 2000 775 1525 \nQ 775 1050 962 787 \nQ 1150 525 1525 525 \nQ 1825 525 2037 787 \nQ 2250 1050 2250 1525 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#SimHei-6d\"/>\n      <use xlink:href=\"#SimHei-65\" x=\"50\"/>\n      <use xlink:href=\"#SimHei-61\" x=\"100\"/>\n      <use xlink:href=\"#SimHei-6e\" x=\"150\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"200\"/>\n      <use xlink:href=\"#SimHei-30\" x=\"250\"/>\n      <use xlink:href=\"#SimHei-2c\" x=\"300\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"350\"/>\n      <use xlink:href=\"#SimHei-73\" x=\"400\"/>\n      <use xlink:href=\"#SimHei-74\" x=\"450\"/>\n      <use xlink:href=\"#SimHei-64\" x=\"500\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"550\"/>\n      <use xlink:href=\"#SimHei-31\" x=\"600\"/>\n     </g>\n    </g>\n    <g id=\"line2d_29\">\n     <path d=\"M 51.2625 32.95 \nL 61.2625 32.95 \nL 71.2625 32.95 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_16\">\n     <!-- mean 0, std 2 -->\n     <g transform=\"translate(79.2625 36.45) scale(0.1 -0.1)\">\n      <use xlink:href=\"#SimHei-6d\"/>\n      <use xlink:href=\"#SimHei-65\" x=\"50\"/>\n      <use xlink:href=\"#SimHei-61\" x=\"100\"/>\n      <use xlink:href=\"#SimHei-6e\" x=\"150\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"200\"/>\n      <use xlink:href=\"#SimHei-30\" x=\"250\"/>\n      <use xlink:href=\"#SimHei-2c\" x=\"300\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"350\"/>\n      <use xlink:href=\"#SimHei-73\" x=\"400\"/>\n      <use xlink:href=\"#SimHei-74\" x=\"450\"/>\n      <use xlink:href=\"#SimHei-64\" x=\"500\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"550\"/>\n      <use xlink:href=\"#SimHei-32\" x=\"600\"/>\n     </g>\n    </g>\n    <g id=\"line2d_30\">\n     <path d=\"M 51.2625 46.2 \nL 61.2625 46.2 \nL 71.2625 46.2 \n\" style=\"fill: none; stroke-dasharray: 9.6,2.4,1.5,2.4; stroke-dashoffset: 0; stroke: #008000; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_17\">\n     <!-- mean 3, std 1 -->\n     <g transform=\"translate(79.2625 49.7) scale(0.1 -0.1)\">\n      <use xlink:href=\"#SimHei-6d\"/>\n      <use xlink:href=\"#SimHei-65\" x=\"50\"/>\n      <use xlink:href=\"#SimHei-61\" x=\"100\"/>\n      <use xlink:href=\"#SimHei-6e\" x=\"150\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"200\"/>\n      <use xlink:href=\"#SimHei-33\" x=\"250\"/>\n      <use xlink:href=\"#SimHei-2c\" x=\"300\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"350\"/>\n      <use xlink:href=\"#SimHei-73\" x=\"400\"/>\n      <use xlink:href=\"#SimHei-74\" x=\"450\"/>\n      <use xlink:href=\"#SimHei-64\" x=\"500\"/>\n      <use xlink:href=\"#SimHei-20\" x=\"550\"/>\n      <use xlink:href=\"#SimHei-31\" x=\"600\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p128314f404\">\n   <rect x=\"42.2625\" y=\"7.2\" width=\"251.1\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 再次使用numpy进行可视化\n",
    "x = np.arange(-7, 7, 0.01)\n",
    "\n",
    "# 均值和标准差对\n",
    "params = [(0, 1), (0, 2), (3, 1)]\n",
    "d2l.plot(x, [normal(x, mu, sigma) for mu, sigma in params], xlabel='x',\n",
    "         ylabel='p(x)', figsize=(4.5, 2.5),\n",
    "         legend=[f'mean {mu}, std {sigma}' for mu, sigma in params])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T08:39:44.656431Z",
     "start_time": "2024-03-28T08:39:44.370300Z"
    }
   },
   "id": "e7729ee812420d99",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "b2ed0c47e6cccdeb"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
